<?php

/**
 * Pes for PHP 5.3+
 *
 * Copyright (c) 2013 phpeskill.com (http://www.pescms.com)
 *
 * For the full copyright and license information, please view
 * the file LICENSE.md that was distributed with this source code.
 */

namespace App\Admin;

use Model\Admin\MenuModel as MenuModel;
use Core\Cache\FileCache as FileCache;

/**
 * Pes 登陆模块
 * @author LazyPHP
 * @version 1.0
 */
class Index extends Common {

    public function __construct() {
        parent::__construct();
    }

    /**
     * 后台首页基本框架
     */
    public function index() {
        $this->display();
    }

    /**
     * 注销登陆
     */
    public function logout() {
        session_unset();
        $this->success('您已经安全退出了系统!', '/');
    }

    /**
     * 获取系统信息
     */
    public function systemInfo() {
        if (false != ($sendmail_path = ini_get('sendmail_path'))) {
            $sysMail = 'Unix Sendmail ( Path: ' . $sendmail_path . ')';
        } elseif (false != ($SMTP = ini_get('SMTP'))) {
            $sysMail = 'SMTP ( Server: ' . $SMTP . ')';
        } else {
            $sysMail = 'Disabled';
        }
        $db = $this->db('option');
        $version = $db->where('id = 13')->find();
        $sysinfo = array(
            'wind_version' => $version['value'],
            'php_version' => PHP_VERSION,
            'server_software' => $_SERVER['SERVER_SOFTWARE'],
            'mysql_version' => @mysql_get_server_info(),
            'max_upload' => ini_get('file_uploads') ? ini_get('upload_max_filesize') : 'Disabled',
            'max_excute_time' => intval(ini_get('max_execution_time')) . ' seconds',
            'sys_mail' => $sysMail);
        $this->assign('sysinfo', $sysinfo);
        $this->display();
    }

    /**
     * 系统菜单
     */
    public function menu() {
        $menu = json_decode(MenuModel::getMenu(), true);
        $topMenu = MenuModel::topMenu();
        $this->assign('menu', $menu);
        $this->assign('topMenu', $topMenu);
        $this->display();
    }

    /**
     * 添加菜单
     */
    public function addMenu() {
        $data = self::menuData();
        $db = $this->db('menu');
        $result = $db->add($data);
        if ($result == false) {
            $this->error('添加菜单失败');
        } else {
            $this->success('成功添加菜单');
        }
    }

    /**
     * 编辑菜单
     */
    public function editMenu() {
        $data = self::ajaxMethod($_POST['key'], $_POST['value']);
        self::ajaxExistParam('id', $data, '请选择要修改的菜单');
        $db = $this->db('menu');
        $findMenu = $db->where('id = :id')->find($data);
        $this->ajaxReturn($findMenu);
    }

    /**
     * 更新菜单
     */
    public function updateMenu() {
        $data = self::menuData('edit');
        $db = $this->db('menu');
        $result = $db->where('id = :id')->save($data);
        if ($result == false) {
            $this->error('更新菜单失败');
        } else {
            $this->success('成功更新菜单');
        }
    }

    public function deleteMenu() {
        $data = self::ajaxMethod($_POST['key'], $_POST['value']);
        self::ajaxExistParam('id', $data, '请选择要删除的菜单');
        $db = $this->db('menu');
        $result = $db->where('id = :id')->delete($data);
        if ($result == false) {
            $this->error('删除菜单失败');
        } else {
            $this->success('删除菜单成功');
        }
    }

    /**
     * 判断和处理提交的系统菜单数据
     * @param str $operating 为空则添加|edit为编辑
     * @return array 返回处理好的数组
     */
    private static function menuData($operating = '') {
        $data = self::ajaxMethod($_POST['key'], $_POST['value'], 'url');
        self::ajaxExistParam('pid', $data, '请选择父菜单');
        self::ajaxExistParam('value', $data, '请填写英文名称');
        self::ajaxExistParam('name', $data, '请选择中文名称');
        if ($data['pid'] == 0) {
            $data['parent'] = 'root';
        } else {
            $db = self::db('menu');
            $pid['pid'] = $data['pid'];
            $getParent = $db->where('id = :pid')->find($pid);
            if ($getParent == false) {
                self::error('获取父类菜单出错');
            } else {
                $data['parent'] = $getParent['value'];
            }
        }
        if ($operating == 'edit') {
            self::ajaxExistParam('id', $data, '请选择要修改的菜单');
        }
        if ($data['pid'] > 0) {
            self::ajaxExistParam('url', $data, '请填写菜单地址');
        }
        return $data;
    }

    /**
     * 清空缓存
     * @param type $dirName 删除的目录
     */
    public function clear($dirName = 'Temp') {
        if ($handle = opendir("$dirName")) {
            while (false !== ($item = readdir($handle))) {
                if ($item != "." && $item != "..") {
                    if (is_dir("$dirName/$item")) {
                        $this->clear("$dirName/$item");
                    } else {
                        if (!unlink("$dirName/$item")) {
                            $this->error("删除文件失败： $dirName/$item");
                        }
                    }
                }
            }
            closedir($handle);
            if ($dirName == 'Temp') {
                $this->success('缓存清空成功!', '/?g=Admin&m=Index&a=systeminfo');
            }
			if (!rmdir($dirName)) {
                $this->error("删除目录失败： $dirName");
            }
        }
    }

}
